<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Persistent Connections (version up to 1.1.4 *only*)</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mongo.connecting.pools.html">Connection Pooling (version 1.2.0-1.2.12 *only*)</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongo.writes.html">Writes</a></div>
 <div class="up"><a href="mongo.connecting.html">链接服务器</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="mongo.connecting.persistent" class="section">
  <h2 class="title">Persistent Connections (version up to 1.1.4 *only*)</h2>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    This section is not relevant for 1.2.0+. In 1.2.0+, connections are always
    persistent and managed automatically by the driver. If you are using a
    1.2.x release (but not 1.3.x or later), see
    <a href="class.mongopool.html" class="classname">MongoPool</a> for more information on pooling.
   </p>
  </p></blockquote>

  <p class="para">
   Creating new connection to the database is very slow. To minimize the number
   of connections that you need to make, you can use persistent connections. A
   persistent connection is saved by PHP, so you can use the same connection for
   multiple requests.
  </p>

  <p class="para">
   For example, this simple program connects to the database 1000 times:
  </p>

  <div class="example" id="mongo.connecting.no-persistent-example">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">for&nbsp;(</span><span style="color: #0000BB">$i</span><span style="color: #007700">=</span><span style="color: #0000BB">0</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$i</span><span style="color: #007700">&lt;</span><span style="color: #0000BB">1000</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$i</span><span style="color: #007700">++)&nbsp;{<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$m&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">();<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>

  <p class="para">
   It takes approximately 18 seconds to execute. If we change it to use a
   persistent connection:
  </p>

  <div class="example" id="mongo.connecting.persistent-example">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">for&nbsp;(</span><span style="color: #0000BB">$i</span><span style="color: #007700">=</span><span style="color: #0000BB">0</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$i</span><span style="color: #007700">&lt;</span><span style="color: #0000BB">1000</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$i</span><span style="color: #007700">++)&nbsp;{<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$m&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">(</span><span style="color: #DD0000">"localhost:27017"</span><span style="color: #007700">,&nbsp;array(</span><span style="color: #DD0000">"persist"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"x"</span><span style="color: #007700">));<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>

  <p class="para">
   ...it takes less than .02 seconds to execute, as it only makes one database
   connection.
  </p>

  <p class="para">
   Persistent connections need an identifier string (which is &quot;x&quot; in the above
   example) to uniquely identify them. For a persistent connection to be used,
   the hostname, port, persist string, and authentication credentials (username,
   password and database, if given) must match an existing persistent
   connection. Otherwise, a new connection will be created with this identifying
   information.
  </p>
  <p class="para">
   Persistent connections are <em class="emphasis">highly recommended</em> and should
   always be used in production unless there is a compelling reason not to.
   Most of the reasons that they are not recommended for relational databases
   are irrelevant to MongoDB.
  </p>
 </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mongo.connecting.pools.html">Connection Pooling (version 1.2.0-1.2.12 *only*)</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongo.writes.html">Writes</a></div>
 <div class="up"><a href="mongo.connecting.html">链接服务器</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
